<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客登陆页</title>

    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/login.css">
    <style>
        body {
            background-color: #f5f7fa;
            font-family: 'Arial', sans-serif;
        }
        
        .nav {
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            background: linear-gradient(to right, #3494e6, #ec6ead);
        }
        
        .blog-title {
            font-weight: bold;
            color: white;
            text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.3);
        }
        
        .nav-span {
            color: white;
            font-weight: 500;
            transition: all 0.3s ease;
            padding: 8px 15px;
            border-radius: 20px;
        }
        
        .nav-span:hover {
            background-color: rgba(255, 255, 255, 0.2);
            transform: translateY(-2px);
        }
        
        .container-login {
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: calc(100vh - 100px);
            padding: 20px;
        }
        
        .login-dialog {
            background-color: white;
            border-radius: 10px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
            padding: 40px;
            width: 450px;
            transition: all 0.3s ease;
        }
        
        .login-dialog:hover {
            box-shadow: 0 15px 35px rgba(0, 0, 0, 0.15);
        }
        
        .login-dialog h3 {
            text-align: center;
            color: #333;
            font-size: 28px;
            margin-bottom: 30px;
            font-weight: 600;
        }
        
        .row {
            margin-bottom: 20px;
            position: relative;
        }
        
        .row span {
            display: block;
            margin-bottom: 8px;
            color: #555;
            font-weight: 500;
        }
        
        .row input {
            width: 100%;
            padding: 12px 15px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 16px;
            transition: all 0.3s;
        }
        
        .row input:focus {
            border-color: #3494e6;
            box-shadow: 0 0 0 2px rgba(52, 148, 230, 0.2);
            outline: none;
        }
        
        button {
            cursor: pointer;
            padding: 12px 24px;
            border: none;
            border-radius: 5px;
            font-size: 16px;
            font-weight: 500;
            transition: all 0.3s;
        }
        
        #submit {
            background: linear-gradient(to right, #3494e6, #ec6ead);
            color: white;
            width: 100%;
            margin-bottom: 15px;
        }
        
        #submit:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(52, 148, 230, 0.3);
        }
        
        #toRegister {
            background-color: #f2f2f2;
            color: #555;
            width: 100%;
        }
        
        #toRegister:hover {
            background-color: #eaeaea;
            transform: translateY(-2px);
        }
        
        .row:last-child {
            margin-bottom: 0;
        }
        
        .error-message {
            color: #e74c3c;
            text-align: center;
            margin-top: 15px;
            font-size: 14px;
            font-weight: 500;
            min-height: 20px;
        }
    </style>
</head>

<body>
<div class="nav">
    <img src="pic/logo2.jpg" alt="">
    <span class="blog-title">我的博客系统</span>
    <div class="space"></div>
    <a class="nav-span" href="blog_list.html">主页</a>
    <a class="nav-span" href="blog_edit.html">写博客</a>
</div>

<div class="container-login">
    <div class="login-dialog">
        <h3>登录</h3>
        <div class="row">
            <span>用户名</span>
            <input type="text" name="username" id="username" placeholder="请输入您的用户名">
        </div>
        <div class="row">
            <span>密码</span>
            <input type="password" name="password" id="password" placeholder="请输入您的密码">
        </div>
        <div class="row">
            <button id="submit" onclick="login()">登录</button>
        </div>
        <div class="row">
            <button id="toRegister" onclick="location.href='register.html'">注册新用户</button>
        </div>
        <div id="error-message" class="error-message"></div>
    </div>
</div>
<script src="js/jquery.min.js"></script>
<script>
    function login() {
        console.log("用户名：",$("#username").val())
        $.ajax({
            type: "post",
            url: "/user/login",
            contentType: "application/json",
            data: JSON.stringify({
                "username": $("#username").val(),
                "password": $("#password").val()
            }),
            success: function(result){
                if(result!=null && result.code==200 && result.data!=null){
                    let userInfo = result.data;
                    //存储token
                    localStorage.setItem("user_token", userInfo.token);
                    localStorage.setItem("login_user_id", userInfo.userId);
                    location.href = "blog_list.html";
                }else {
                    $("#error-message").text(result.errMsg || "登录失败");
                }
            },
            error: function() {
                $("#error-message").text("登录请求失败，请稍后重试");
            }
        });
    }
</script>
</body>

</html>